#include "fruit.h"
#include <string>
#include <time.h>


//////////////////////////////////////////////////////////////////////////
void fromServer(unsigned char* ptr, unsigned long& cid, int& order, char& type)
{
	cid = *(unsigned long*)(ptr);
	order = *(int*)(ptr + 4);
	type = *(ptr + 8);
}


void fromClient(unsigned char* ptr, unsigned long& t_or_cid, int& order, int& orderInner, int& parameter, int& roleId, char& type)
{
	t_or_cid = *(unsigned long*)(ptr);
	order = *(int*)(ptr + 4);
	orderInner = *(int*)(ptr + 8);
	parameter = *(int*)(ptr + 12);
	roleId = *(int*)(ptr + 16);
	type = *(ptr + 20);
}


unsigned char* toServer(int offt, int order, int orderInner, int parameter, int roleId, char type, const unsigned char* arr, int& length)
{
	length += c21;
	auto arr2 = new unsigned char[length];
	*(unsigned long*)(arr2) = time(nullptr) + offt;
	*(int*)(arr2 + 4) = order;
	*(int*)(arr2 + 8) = orderInner;
	*(int*)(arr2 + 12) = parameter;
	*(int*)(arr2 + 16) = roleId;
	*(arr2 + 20) = type;
	if (arr != nullptr && length > 0)
	{
		memcpy(arr2 + c21, arr, length - c21);
	}
	return arr2;
}


unsigned char* toGate(unsigned long cid, int order, char type, const unsigned char* arr, int& length)
{
	length += c9;
	auto arr2 = new unsigned char[length];
	*(int*)(arr2) = cid;
	*(int*)(arr2 + 4) = order;
	*(arr2 + 8) = type;
	if (arr != nullptr && length > 0)
	{
		memcpy(arr2 + c9, arr, length - c9);
	}
	return arr2;
}


unsigned char* toClient(int order, char type, const unsigned char* arr, int& length)
{
	length += 5;
	auto arr2 = new unsigned char[length];
	*(int*)(arr2) = order;
	*(arr2 + 4) = type;
	if (arr != nullptr && length > 0)
	{
		memcpy(arr2 + 5, arr, length - 5);
	}
	return arr2;
}
